package com.qtjf.qtmoneyservice.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.qtjf.qtmoneyservice.R;

public class WebViewActivity extends BaseActivity {

    WebView webView;
    String url;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        Intent intent=getIntent();
        url=intent.getStringExtra("url");
      //  url="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=1417751808&token=&lang=zh_CN";

        initView();
        initData();
        initEvent();
    }

    private void initView() {
        webView= (WebView) findViewById(R.id.web_view);
    }

    private void initData() {
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
//url是要加载的地址
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
               // view.loadUrl("JavaScript:function setTop(){document.querySelector('#BAIDU_SSP__wrapper_u2806589_0').style.display=\"none\";}setTop();");
//根据js语法,从获取的html中去除指定的块，可用于去除网页中的广告
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
//网页加载失败后的回调
                super.onReceivedError(view, request, error);
            }
        });



        //webView.loadUrl("http://m.xzw.com/shengxiao/zonghe/");
        webView.loadUrl(url);//指定当前页面地址
    }

    private void initEvent() {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        webView.stopLoading();
        webView.destroy();
        webView=null;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK ) {

            if (!webView.canGoBack()) {
                WebViewActivity.this.finish();

            } else {
                webView.goBack();// 返回前一个页面

            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
